//
//  LookViewController.m
//  PigMarket
//
//  Created by mac on 2019/7/2.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "LookViewController.h"
#import "LookPigInfoView.h"
#import "LookFriendsCell.h"
#import "PigRecordViewController.h"
#import "LimitsOfAuthorityViewController.h"
#import "ChatViewController.h"
#import "MonitoringView.h"
#import "AppDelegate.h"
//#import <EZOpenSDKFramework/EZOpenSDKFramework.h>
#import "SharpnessMenuView.h"


//@interface LookViewController ()<UITableViewDelegate,UITableViewDataSource,EZPlayerDelegate>
///**
// player
// */
//@property (nonatomic , strong) EZPlayer *player;
///**
// myTableView
// */
//@property (nonatomic , strong) UITableView *myTableView;
///**
// dataArr
// */
//@property (nonatomic , strong) NSMutableArray *dataArr;
///**
// 监控整体bgview
// */
//@property (nonatomic , strong) UIView *playBgView;
///**
// 监控bgview
// */
//@property (nonatomic , strong) UIView *playView;
///**
// 监控菜单
// */
//@property (nonatomic , strong) MonitoringView *playerMenuView;
///**
// menu
// */
//@property (nonatomic , strong) SharpnessMenuView *sharpnessMenuView;
///**
// 猪的信息
// */
//@property (nonatomic , strong) LookPigInfoView *pigInfoView;
///**
// currentPage
// */
//@property (nonatomic , assign) int currentPage;
///**
// 附近的豚友总人数
// */
//@property (nonatomic , strong) NSNumber *totalFriendNum;
///**
// 监控数组
// */
//@property (nonatomic , strong) NSMutableArray *playerDataArr;
///**
// 序列号
// */
//@property (nonatomic , strong) NSString *deviceSerial;
///**
// 通道号
// */
//@property (nonatomic , assign) NSUInteger cameraNo;
///**
// 当前显示摄像头
// */
//@property (nonatomic , assign) int currentCamera;
//@end
//
@implementation LookViewController
//
//
//#pragma mark - view life cycle
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    [self setUpNav];
//    [self setUpView];
//
//}
//
//-(void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//}
//
//-(void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//}
//- (void)viewDidLayoutSubviews {
//    [super viewDidLayoutSubviews];
//    if (self.playerMenuView.fullScreenBtn.selected) {
//        self.pigInfoView.hidden = YES;
//        self.myTableView.hidden = YES;
//        [self.playerMenuView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.bottom.mas_equalTo(0);
//            make.height.mas_equalTo(50);
//        }];
//        [self.playBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.top.mas_equalTo(-TopBarHeight);
//            make.bottom.mas_equalTo(self.playerMenuView.mas_top);
//            make.height.mas_equalTo(ScaleHeight(232));
//        }];
//
//    } else {
//        self.pigInfoView.hidden = NO;
//        self.myTableView.hidden = NO;
//        [self.playBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.top.mas_equalTo(-TopBarHeight);
//            make.height.mas_equalTo(ScaleHeight(232));
//        }];
//        [self.playerMenuView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.top.mas_equalTo(self.playBgView.mas_bottom);
//            make.height.mas_equalTo(ScaleHeight(40));
//        }];
//    }
//}
//#pragma mark - init method
//- (void)setUpNav {
//    [[NavigationBarManager sharedInstance] setupNavigationBarClear:self.navigationController];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"adopt_adoptdetail_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"user_setup"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];
//}
//
//- (void)setUpView {
//    [self initView];
//    [self initLayout];
//    [self initAction];
//    [self initData];
//}
//- (void)initView {
//    [self.view addSubview:self.playBgView];
//    [self.view addSubview:self.playerMenuView];
//    [self.view addSubview:self.pigInfoView];
//    [self.view addSubview:self.myTableView];
//
//    [self.view addSubview:self.sharpnessMenuView];
//}
//
//- (void) initLayout {
//
//    [self.playBgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(-TopBarHeight);
//        make.height.mas_equalTo(ScaleHeight(232));
//    }];
//    [self.playerMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(self.playBgView.mas_bottom);
//        make.height.mas_equalTo(ScaleHeight(40));
//    }];
//    [self.pigInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.playerMenuView.mas_bottom);
//        make.left.right.mas_equalTo(0);
//        make.height.mas_equalTo(148);
//    }];
//    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.pigInfoView.mas_bottom);
//        make.left.right.bottom.mas_equalTo(0);
//    }];
//    [self.sharpnessMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.playerMenuView.changeSharpnessBtn.mas_centerX);
//        make.bottom.mas_equalTo(self.playerMenuView.mas_top);
//        make.width.mas_equalTo(ScaleWidth(80));
//    }];
//}
//- (void) initAction {
//    [self.pigInfoView.recordBtn addTarget:self action:@selector(recordBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.pigInfoView.foodBookBtn addTarget:self action:@selector(foodBookBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.playerMenuView.playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.playerMenuView.changeCameraBtn addTarget:self action:@selector(changeCameraBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.playerMenuView.changeSharpnessBtn addTarget:self action:@selector(changeSharpnessBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.playerMenuView.fullScreenBtn addTarget:self action:@selector(fullScreenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//
//    @weakify(self);
//    [self.sharpnessMenuView setSharpnessMenuBlock:^(SharpnessMenuType sharpnessMenuType) {
//        @strongify(self);
//        NSString *title = nil;
//        EZVideoLevelType videoLevel = EZVideoLevelLow;
//        switch (sharpnessMenuType) {
//            case SharpnessMenuTypeNormal:
//            {
//                title = @"标清";
//                videoLevel = EZVideoLevelMiddle;
//            }
//                break;
//            case SharpnessMenuTypeFluid:
//            {
//                title = @"流畅";
//                videoLevel = EZVideoLevelLow;
//            }
//                break;
//            case SharpnessMenuTypeHD:
//            {
//                title = @"高清";
//                videoLevel = EZVideoLevelHigh;
//            }
//                break;
//            default:
//                break;
//        }
//        //切换清晰度萤石云请求
//        [self changeSharpnessWithVideoLevel:videoLevel];
//
//        [self.playerMenuView.changeSharpnessBtn setTitle:title forState:UIControlStateNormal];
//        self.sharpnessMenuView.hidden = YES;
//        self.playerMenuView.changeSharpnessBtn.selected = NO;
//    }];
//    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self.dataArr removeAllObjects];
//        self.currentPage = 1;
//        [self samePenFriendRequest];
//    }];
//    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        self.currentPage ++;
//        [self samePenFriendRequest];
//    }];
//    self.myTableView.mj_footer.hidden = YES;
//}
//
//- (void)initData {
//    self.currentCamera = 1;
//    self.currentPage = 1;
//    [self pigInfoRequest];
//    [self samePenFriendRequest];
//}
//
//#pragma mark - delegate
//#pragma mark - tableview delegate
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.dataArr.count;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 42;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 0.01;}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42)];
//    headerView.backgroundColor = [UIColor globalBackgroundColor];
//    UILabel *tipsLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH/2, 42)];
//    tipsLab.text = [NSString stringWithFormat:@"同栏豚友(%@)",self.totalFriendNum];
//    tipsLab.textColor = [UIColor tipsGrayColor];
//    tipsLab.font = [UIFont systemFontOfSize:16];
//    [headerView addSubview:tipsLab];
//    return headerView;
//}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    return nil;
//}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    LookFriendsCell *cell = [LookFriendsCell cellWithTableView:tableView];
//    if (self.dataArr.count > indexPath.row) {
//        SamePenFriend *model = self.dataArr [indexPath.row];
//        cell.model = model;
//        @weakify(self);
//        [cell setAddFriendOrChatBlock:^{
//            @strongify(self);
//            if ([model.friendIf integerValue] == 0) {//不是好友
//                [self addFriendWithIdentifier:model.memberNum];
//            } else if ([model.friendIf integerValue] == 1 ) {//是好友
//
//                [self beiginChatWithIdentifier:model.nickName];
//            }
//        }];
//    }
//
//    return cell;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//
//}
//#pragma mark - player delegate
////播放器失败
//- (void)player:(EZPlayer *)player didPlayFailed:(NSError *)error {
//    if (error.code == EZ_SDK_PARAM_ERROR) {
//        [self toastWithContent:@"接口参数错误"];
//    } else if (error.code == EZ_SDK_TIMEOUT){
//        [self toastWithContent:@"无播放token"];
//    } else if (error.code == EZ_SDK_NEED_VALIDATECODE){
//        [self toastWithContent:@"需要设备验证码"];
//    } else if (error.code == EZ_SDK_VALIDATECODE_NOT_MATCH){
//        [self toastWithContent:@"设备验证码不匹配"];
//    } else if (error.code == EZ_SDK_STREAM_TIMEOUT){
//        [self toastWithContent:@"取流超时,请检查手机网络"];
//    }
//}
////播放器消息回调
//- (void)player:(EZPlayer *)player didReceivedMessage:(NSInteger)messageCode {
//    if (messageCode == PLAYER_NO_NETWORK) {
//        [self toastWithContent:@"无网络"];
//    } else if (messageCode == PLAYER_VIDEOLEVEL_CHANGE){
//        [self toastWithContent:@"清晰度切换中"];
//    } else if (messageCode == PLAYER_STREAM_RECONNECT){
//        [self toastWithContent:@"正在重连"];
//    }
//}
//#pragma mark - target action
//- (void)leftButtonClick {
//    if (self.playerMenuView.fullScreenBtn.selected) {
//        self.playerMenuView.fullScreenBtn.selected = NO;
//        [self changeScreenMode:NO];
//    } else {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}
////设置
//- (void)rightButtonClick {
//    if (self.playerMenuView.fullScreenBtn.selected) {
//        self.playerMenuView.fullScreenBtn.selected = NO;
//        [self changeScreenMode:NO];
//    }
//    LimitsOfAuthorityViewController *vc = [[LimitsOfAuthorityViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
// }
////仔猪档案
//- (void)recordBtnClick {
//    PigRecordViewController *vc = [[PigRecordViewController alloc] init];
//    vc.pigId = self.pigId;
//    [self.navigationController pushViewController:vc animated:YES];
//}
////他的食谱
//- (void)foodBookBtnClick {
//
//}
////播放
//- (void)playBtnClick:(UIButton *)button {
//    button.selected = !button.selected;
//    if (button.selected) {
//        [self.player stopRealPlay];
//    } else {
//        [self.player startRealPlay];
//    }
//}
////切换摄像头
//- (void)changeCameraBtnClick:(UIButton *)button {
//
//    if (self.playerDataArr.count > self.currentCamera) {
//        self.currentCamera ++;
//     }else {
//        self.currentCamera = 1;
//    }
//    PMLog(@"-----%d",self.currentCamera);
//    Look *model = self.playerDataArr[self.currentCamera - 1];
//    self.deviceSerial = model.serialNumber;
//    self.cameraNo = [model.cameraId integerValue];
//    self.player = [EZOpenSDK createPlayerWithDeviceSerial:self.deviceSerial cameraNo:self.cameraNo];
//    [self.player startRealPlay];
//}
////切换清晰度
//- (void)changeSharpnessBtnClick:(UIButton *)button {
//    button.selected = !button.selected;
//    self.sharpnessMenuView.hidden = !button.selected;
//}
////全屏
//- (void)fullScreenBtnClick:(UIButton *)button {
//    button.selected = !button.selected;
//    [self changeScreenMode:button.selected];
//}
///**
// 强制横屏
//
// @param isFullscreen YES: 横屏， NO: 竖屏
// */
//- (void)changeScreenMode:(BOOL)isFullscreen {
//    AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication.delegate;
//    appDelegate.allowRotation = isFullscreen;
//    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
//    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
//    NSNumber *orientationTarget;
//    if (isFullscreen) {
//        orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
//    } else {
//        orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
//    }
//    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//}
//
//#pragma mark - private method
//- (void)pigInfoRequest {
//     @weakify(self);
//    [[[PigMarketApi sharedInstance] getLookDetailInfoWithPigId:self.pigId] subscribeNext:^(LookList * _Nullable lookInfo) {
//        @strongify(self);
//        [self.pigInfoView.headImg sd_setImageWithURL:[NSURL URLWithString:lookInfo.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
//        self.pigInfoView.titleLab.text = lookInfo.varietiesName;
//        self.pigInfoView.numLab.text = [NSString stringWithFormat:@" 编号:%@ ",lookInfo.pigNum];
//        self.pigInfoView.supervisePeopleNumLab.text = [NSString stringWithFormat:@"监督人次:%@",lookInfo.superviseCount];
//        if ([lookInfo.advanceSaleIf integerValue]== SaleTypeYes) {
//            [self.pigInfoView.toSaleBtn setTitle:@"去预售" forState:UIControlStateNormal];
//
//        } else if ([lookInfo.advanceSaleIf integerValue] == SaleTypeNo) {
//            [self.pigInfoView.toSaleBtn setTitle:@"预售过" forState:UIControlStateNormal];
//        }
//        [self.playerDataArr addObjectsFromArray:lookInfo.cameraList];
//
//        //初始化监控
//        [self configPlayerWithAccessToken:lookInfo.token];
//
//    } error:^(NSError * _Nullable error) {
//
//        @strongify(self);
//        [self handlingError:error];
//    }];
//}
////同栏豚友请求
//- (void)samePenFriendRequest {
//
//    @weakify(self);
//    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
//    [[[PigMarketApi sharedInstance] samePenFriendWithPigId:self.pigId memberId:memberId page:self.currentPage] subscribeNext:^(SamePenFriendList * _Nullable samePenFriendList) {
//        @strongify(self);
//        self.totalFriendNum = samePenFriendList.totalCount;
//        [self.dataArr addObjectsFromArray:samePenFriendList.list];
//        [self.myTableView reloadData];
//        [self.myTableView.mj_header endRefreshing];
//        [self.myTableView.mj_footer endRefreshing];
//        if (self.currentPage == [samePenFriendList.totalPage intValue] || [samePenFriendList.totalPage intValue] == 0) {
//            [self.myTableView.mj_footer endRefreshingWithNoMoreData];;
//        } else {
//            self.myTableView.mj_footer.hidden = NO;
//        }
//    } error:^(NSError * _Nullable error) {
//        @strongify(self);
//        [self handlingError:error];
//        [self.myTableView.mj_header endRefreshing];
//        [self.myTableView.mj_footer endRefreshing];
//    }];
//}
////初始化监控信息
//- (void)configPlayerWithAccessToken:(NSString *)accessToken{
//    if (self.playerDataArr.count > 0) {
//        Look *model = self.playerDataArr[self.currentCamera - 1];
//        [EZOpenSDK setAccessToken:accessToken];
//        self.deviceSerial = model.serialNumber;
//        self.cameraNo = [model.cameraId integerValue];
//        self.player = [EZOpenSDK createPlayerWithDeviceSerial:self.deviceSerial cameraNo:self.cameraNo];
//        self.player.delegate = self;
//        [self.player setPlayerView:self.playBgView];
//        [self.player startRealPlay];
//    }
//}
////切换清晰度请求
//- (void)changeSharpnessWithVideoLevel:(EZVideoLevelType)videoLevel {
//    @weakify(self);
//    [EZOpenSDK setVideoLevel:self.deviceSerial cameraNo:self.cameraNo videoLevel:videoLevel completion:^(NSError *error) {
//        @strongify(self);
//        if ([JTool iSNull:error]) {
//            [self.player stopRealPlay];
//            [self.player startRealPlay];
//        } else {
//            [self toastWithContent:@"切换失败，请稍后再试"];
//        }
//    }];
//}
////添加好友
//- (void)addFriendWithIdentifier:(NSString *)identifier {
//    AddFriendVerifyViewController *vc = [[AddFriendVerifyViewController alloc] init];
//    vc.identifier = identifier;
//    [self.navigationController pushViewController:vc animated:YES];
//}
////聊天
//- (void)beiginChatWithIdentifier:(NSString *)identifier {
//    TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
//    data.convId = identifier;
//    data.convType = TIM_C2C;
//    data.title = identifier;
//    ChatViewController *chat = [[ChatViewController alloc] init];
//    chat.conversation = data;
//    [self.navigationController pushViewController:chat animated:YES];
//}
//
//#pragma mark - getter and setter
//- (UITableView *)myTableView {
//    if (!_myTableView) {
//        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
//        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _myTableView.rowHeight = 70;
//        _myTableView.delegate = self;
//        _myTableView.dataSource = self;
//    }
//    return _myTableView;
//}
//- (NSMutableArray *)dataArr {
//    if (!_dataArr) {
//        _dataArr = [[NSMutableArray alloc] init];
//    }
//    return _dataArr;
//}
//- (NSMutableArray *)playerDataArr {
//    if (!_playerDataArr) {
//        _playerDataArr = [[NSMutableArray alloc] init];
//    }
//    return _playerDataArr;
//}
//- (UIView *)playBgView {
//    if (!_playBgView) {
//        _playBgView = [[UIView alloc] init];
//        _playBgView.backgroundColor = [UIColor blackColor];
//    }
//    return _playBgView;
//}
//- (MonitoringView *)playerMenuView {
//    if (!_playerMenuView) {
//        _playerMenuView = [[MonitoringView alloc] init];
//        _playerMenuView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.73];
//    }
//    return _playerMenuView;
//}
//- (LookPigInfoView *)pigInfoView {
//    if (!_pigInfoView) {
//        _pigInfoView = [[LookPigInfoView alloc] init];
//        _pigInfoView.backgroundColor = [UIColor whiteColor];
//    }
//   return _pigInfoView;
//}
//- (SharpnessMenuView *)sharpnessMenuView {
//    if (!_sharpnessMenuView) {
//        _sharpnessMenuView = [[SharpnessMenuView alloc] init];
//        _sharpnessMenuView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.73];
//        _sharpnessMenuView.hidden = YES;
//    }
//    return _sharpnessMenuView;
//}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    self.sharpnessMenuView.hidden = YES;
//}
//- (void)dealloc {
//    [EZOpenSDK releasePlayer:self.player];
//}
//
@end

